<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/10
 * Time: 17:26
 */

namespace App\Services;

use App\Models\LoopImg;

class LoopImgService extends BaseService
{
    protected $modelClass = LoopImg::class;

    /**
     * 添加
     * @param $postData
     * @return bool|string
     * @throws \Exception
     */
    public function add($postData)
    {
        $postData['uuid'] = uuid();

        return $this->saveData($postData);
    }

    /**
     * 编辑
     * @param $uuid
     * @param $postData
     * @return mixed
     * @throws \Exception
     */
    public function edit($uuid, $postData)
    {
        try {
            return $this->findByUuidOrFail($uuid)->update(filter_null($postData));
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 详情
     * @param $uuid
     * @return mixed
     * @throws \Exception
     */
    public function detail($uuid)
    {
        try {
            return $this->model->where('uuid', $uuid)
                ->with('imgAttach')
                ->first();
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 删除
     * @param $uuid
     * @return mixed
     * @throws \Exception
     */
    public function remove($uuid)
    {
        try {
            return $this->removeByUuid($uuid);
        } catch (\Exception $e) {
            throw new \Exception('删除失败！');
        }
    }

    /**
     * @desc 列表
     * @param array $search
     * @param int $per_page
     * @return array
     */
    public function getList($search=[], $per_page=20)
    {
        $columns = ['*'];

        $model = $this->searcher($this->model, $search)->with('imgAttach');

        $conf = [
            'search'  => 'conf_search.loopImg_getList',
            'columns' => 'conf_columns.loopImg_getList',
            'buttons' => 'conf_buttons.loopImg_getList',
        ];
        return $this->get_paginate($model->paginate($per_page, $columns), $conf);
    }

}